package edu.imac.angryfood.states;


import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.MouseOverArea;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import edu.imac.angryfood.game.Angryfood;
import edu.imac.angryfood.utils.Account;


public class SelectLevelState extends BasicGameState implements ComponentListener{
	
	public static int ID;
	public Angryfood parent;
	private int spacing = 60;
	/** The areas defined */
	private MouseOverArea[] areas;
	private MouseOverArea[] areas2;
	private MouseOverArea back_;
	/** The logo of the game */
	private Image logo;
	
	/** The background image */
	private Image background;
	/** The button image */
	private Image cadre;
	/** Images of the menu items */
	private Image france;
	private Image japon; 
	private Image choice; 
	private Image back; 

	/** Account of the current player */
	private Account current_player;
	private StateBasedGame game;
	
	/** Last level of the player, equals to his number of levels in the chapter*/
	private char levels;
	/** Number of levels of a player*/
	int nbLevels;
	
	public SelectLevelState(Angryfood game, int ID) {
		super();
		SelectLevelState.ID = ID;
		this.parent = game;

	}
	
	/** initializes the game
	 * @param container contains the game
	 * @param game the game
	 * @throws SlickException
	 */
	
	public void init(GameContainer container, StateBasedGame game) throws SlickException {
		this.game = game;
		
		/** We initialize all the areas if the boolean is set to false*/
		if(parent.load_player == true){
			/** We get the current player */
				current_player = parent.getCurrentPlayer();
				
			/** We get his current number of level*/
				levels = current_player.getLast_level().charAt(2);
				nbLevels = Character.digit(levels, 10);
				
			/** The chapter of levels is the first, initialization of the areas*/
				if(current_player.getLast_level().charAt(0) == '1'){
					areas = new MouseOverArea[nbLevels];
					cadre = new Image("res/img/menu/cadre.png");
					for (int i=0;i<nbLevels;i++) {	
						areas[i] = new MouseOverArea(container, cadre, 50, 250 + i *spacing, 200, 50, this);
						areas[i].setNormalColor(new Color(1,1,1,0.8f));
						areas[i].setMouseOverColor(new Color(1,1,1,1f));
					}
				}
				
			/** The chapter of levels is the second one, initialization of the areas*/
				else if(current_player.getLast_level().charAt(0) == '2'){
					nbLevels += 3;
					areas = new MouseOverArea[nbLevels];
					areas2 = new MouseOverArea[nbLevels];
					cadre = new Image("res/img/menu/cadre.png");
					for (int i=0;i<nbLevels;i++) {
						if(i<3){
							areas[i] = new MouseOverArea(container, cadre, 50, 250 + i *spacing, 200, 50, this);
							areas[i].setNormalColor(new Color(1,1,1,0.8f));
							areas[i].setMouseOverColor(new Color(1,1,1,1f));
						}
						else {
							areas2[i] = new MouseOverArea(container, cadre, 400, 250 + (i-3) *spacing, 200, 50, this);
							areas2[i].setNormalColor(new Color(1,1,1,0.8f));
							areas2[i].setMouseOverColor(new Color(1,1,1,1f));
						}
					}
				}
				
		}
		
		/** Initialization of the pictures*/
		logo =  new Image("res/img/menu/logoAF.png");
		choice =  new Image("res/img/menu/level_choice.png");
		back =  new Image("res/img/menu/back.png");
		japon =  new Image("res/img/menu/japon.png");
		france =  new Image("res/img/menu/france.png");
		background = new Image("res/img/menu/fondAF.jpg");
		back_ = new MouseOverArea(container, back, 20, 525, 250, 50, this);
		
	}
		
	
	/** render the graphics in slick
	 * @param container contains the game
	 * @param game the game
	 * @param g the graphics
	 */
	


	public void render(GameContainer container, StateBasedGame game, Graphics g) {
		
		/** Draw o the pictures*/
		background.draw(0, 0, 800, 600);
		logo.draw(400,0,400,100);
		choice.draw(30,75,250,50);

		/** Call of the initialization if the boolean is set to false @*/
		if(parent.load_player == false)
		{
			parent.load_player = true;
			try {
				init(container, game);
			} catch (SlickException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		
		/** The chapter of levels is the first, draw of the areas*/
		if(current_player.getLast_level().charAt(0) == '1'){
			
			japon.draw(50,150,200,100);
			for (int i=0;i<nbLevels;i++) {	
				areas[i].setNormalColor(new Color(1,1,1,0.8f));
				areas[i].setMouseOverColor(new Color(1,1,1,1f));
				areas[i].render(container, g);
			    g.drawString("Niveau "+(i+1), 105 , 265 + i *spacing);
			}
		}
		
		/** The chapter of levels is the second one, draw of the areas*/
		else if(current_player.getLast_level().charAt(0) == '2'){
			japon.draw(50,150,200,100);
			france.draw(400,150,200,100);
			for (int i=0;i<(int)nbLevels;i++) {
				if(i<3){
					areas[i].setNormalColor(new Color(1,1,1,0.8f));
					areas[i].setMouseOverColor(new Color(1,1,1,1f));
					areas[i].render(container, g);
				g.drawString("Niveau "+(i+1), 105 , 265 + i *spacing); 
				}
				else {
					areas2[i].setNormalColor(new Color(1,1,1,0.8f));
					areas2[i].setMouseOverColor(new Color(1,1,1,1f));
					areas2[i].render(container, g);
				g.drawString("Niveau "+(i+1), 455 , 265 + (i-3) *spacing); 
				}
			}
		}
			
		
		
		/** Rendering of the return button */
			back_.render(container, g);	
		

		
	}

	/** update the physic world
	 * @param container the game container
	 * @param game the game
	 * @param delta
	 */	
	public void update(GameContainer container, StateBasedGame game, int delta) {
		
	}

	/**
     * Function used to click on the areas
     * 
     * @param source Area of the screen which is clicked
     */
	
	@Override
	public void componentActivated(AbstractComponent source) {	

		for (int i=0;i<(int)nbLevels;i++) {
		
			if(current_player.getLast_level().charAt(0) == '1'){
				if (source == areas[i]) {
					 i = i+1;
					 parent.load_level = false;
					 /** We set the path of the level and change the current level, here : Chapter 1 */
					parent.currentLevel = "res/levels/level1_"+i+".xml";
					
					game.enterState(4, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
				}
			}
			else if(current_player.getLast_level().charAt(0) == '2'){
				if (source == areas[i]) {
					 i = i+1;
					 parent.load_level = false;
					 /** We set the path of the level and change the current level, here : Chapter 1 */
					parent.currentLevel = "res/levels/level1_"+i+".xml";
					
					game.enterState(4, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
				}
				
				if (source == areas2[i]) {
					parent.load_level = false;
					 /** We set the path of the level and change the current level, here : Chapter 2 */
					parent.currentLevel = "res/levels/level2_"+(i-2)+".xml";
					
					game.enterState(4, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
				}
			}
		}
		if(source==back_){
			 /** We go back, we need to reinitialize the state with a new player*/
			parent.load_player = false;
			game.enterState(1, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
			
			
		}
		
		
	}
		
	
		
	

	@Override
	public int getID() {
		return SelectLevelState.ID;
	}


}
